// 页面代码
import {
  validateMobile,
  validateParam
} from '../../validate/validate.js';
import {
  sendCode,
  bindMobile
} from '../../apis/index.js'; // 导入封装的网络请求函数
const userInfo = wx.getStorageSync('userInfo')
Page({
  data: {
    sendtext: "发送验证码",
    mobile: "",
    code: "",
    issendcode: false,
    countdown: 60,
    countdownInterval: null, // 保存倒计时的定时器
    typeMb:"info",
    colorMb:"#fa8072",
    typeCode:"info",
    colorCode:"#fa8072"
  },

  // 监听手机号输入
  handleMobile: function (e) {
    const value = e.detail.value
    this.setData({
      mobile: value,
      typeMb:value.length>10?"success":"info",
      colorMb:value.length>10?"#00b26a":"#fa8072" 
    });

  },

  // 监听验证码输入
  handlecode: function (e) {
    this.setData({
      code: e.detail.value
    });
  },
  handleCode:function(e){
    const value = e.detail.value
    this.setData({
      code: value,
      typeCode:value.length>5?"success":"info",
      colorCode:value.length>5?"#00b26a":"#fa8072" 
    });
  },

  sendCode: function () {
    if (this.data.issendcode){
      wx.showToast({
        title: `${this.data.countdown}后再重新发送`,
        icon:"none"
      })
      return;
    };

    const mobile = this.data.mobile;
    if (!validateMobile(mobile)) return;

    // 开始倒计时
    this.setData({
      issendcode: true,
      sendtext: `${this.data.countdown}秒后重发`
    });
    this.startCountdown();

    sendCode(mobile)
      .then((res) => {
        console.log("请求成功");
        console.log(res);
        wx.showToast({
          title: '验证码已发送~',
          icon: 'success'
        });
      })
      .catch((error) => {
        console.log("请求失败");
        console.log(error);
       if(error.data.error){
         wx.showToast({
           title: error.data.error,
           icon:"none"
         })
       }
       if(error.data.detail){
        wx.showToast({
          title: error.data.detail,
          icon:"none"
        })
       }else{
        wx.showToast({
          title: '网络错误请稍后重试',
          icon: "none"
        });
       }
        this.resetCountdown(); // 停止倒计时
      });
  },

  startCountdown() {
    const countdownInterval = setInterval(() => {
      let countdown = this.data.countdown - 1;
      if (countdown <= 0) {
        this.resetCountdown();
      } else {
        this.setData({
          countdown: countdown,
          sendtext: `${countdown}秒后重发`,
        });
      }
    }, 1000);
    this.setData({
      countdownInterval: countdownInterval
    });
  },

  resetCountdown() {
    if (this.data.countdownInterval) {
      clearInterval(this.data.countdownInterval); // 清除定时器
    }
    this.setData({
      issendcode: false,
      countdown: 60,
      sendtext: '发送验证码',
      countdownInterval: null
    });
  },

  bindmobile: function () {
    const {
      mobile,
      code
    } = this.data;
    if (!validateParam({
        mobile,
        code
      })) return;
    if (!validateMobile(mobile)) return;

    bindMobile(mobile, code)
      .then((res) => {
        console.log("成功");
        console.log(res);
          wx.showToast({
            title: '绑定成功',
            icon: "none"
          });
          userInfo.phone_number=this.data.mobile
      })
      .catch((error) => {
        console.log("错误");
        console.log(error);
        if (error.statusCode === 401) {
          wx.showToast({
            title: '请先登录',
            icon: "none"
          });
          return;
        }
        if (error.data.error){
          wx.showToast({
            title: error.data.error,
            icon:"none"
          })
        }else{
          wx.showToast({
            title: '网络错误',
            icon: "none"
          });
        }
        
      });
  },

  onLoad: function (options) {},
  onReady: function () {},
  onShow: function () {
  },
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
});